package com.qf.controller;

import com.qf.dto.ContractorDto;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.page.Result;
import com.qf.service.ContractorService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/contractor")
@RestController
public class ContractorController {

    @Autowired
    private ContractorService contractorService;



    /**
     * project工程列表分页查询+模糊查询
     * @param queryPageBean
     * @return PageResult
     */
    @RequestMapping("/all")
    public PageResult all(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = contractorService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新建开发商并绑定工程
     * @param  cname, pids
     * @return Result
     */
    //urlencode方式提交参数
    //必须要求参数名字一样
    @RequestMapping("/add")
    public Result add(String cname,int[] pids) {
        Result result = new Result();
        boolean b = contractorService.add(cname,pids);
        if (b){
            result.setFlag(true);
            result.setMessage("添加成功");
            return result;
        }
        result.setFlag(false);
        result.setMessage("添加失败");
        return result;
    }


    /**
     * 编辑contractor回显数据
     * @param cid
     * @return Result
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam("cid") String cid ){
        Result result = new Result();
        ContractorDto contractorDto = contractorService.findById(cid);
        boolean b = ObjectUtils.isNotEmpty(contractorDto);
        if (b){
            result.setData(contractorDto);
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }else {
            result.setFlag(false);
            result.setMessage("fail");
            return result;
        }
    }


    /**
     * 修改contractor并修改绑定project
     * @param  cname, pids, cid
     * @return result
     */
    @PostMapping("/edit")
    public Result edit(String cname,int[] pids,String cid){
        Result result = new Result();
        boolean b = contractorService.edit(cname, pids, cid);
        if (b){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }

    /**
     * 删除contractor并取消绑定对应的project
     * @param cid
     * @return result
     */
    @RequestMapping("/delete")
    public Result delete(@RequestParam("cid") String cid){
        Result result = new Result();
        int i = contractorService.delete(cid);
        if (i>0){
            result.setFlag(true);
            result.setMessage("success");
            return result;
        }
        result.setFlag(false);
        result.setMessage("fail");
        return result;
    }
}
